TOOL.Category = "Turret Weaons"
TOOL.Name = "Missile Turret"
TOOL.Command = nil
TOOL.ConfigName = ""
TOOL.ent = {} 
TOOL.ClientConVar["Power"] = "10"
TOOL.ClientConVar["Type"] = "0"
cleanup.Register( "missileturret" )

if CLIENT then
   language.Add ("Tool_missileturret_name", "Missile Turret")
   language.Add ("Tool_missileturret_desc", "A customizable Missile Turret.")
   language.Add ("Tool_missileturret_0", "Primary: Spawn a Missile Turret")
   language.Add ("Undone_missileturret", "Missile Turret undone.")
end



function TOOL:LeftClick( trace )
	if (CLIENT) then return true end
	local ply = self:GetOwner()
	local Type = self:GetClientNumber( "Type" ) 
	if ( !trace.Hit ) then return end

	if ( SERVER and !util.IsValidPhysicsObject( trace.Entity, trace.PhysicsBone ) ) then return false end
			local Ang = (trace.HitNormal:Angle() + Angle(90,0,0))
			local Pos = trace.HitPos + trace.HitNormal* 8
	   local Power = self:GetClientNumber("power")
	if Type==0 then
		local Weapon =  MakeMissile(ply, Pos, Ang, Power)
		local phys = Weapon:GetPhysicsObject()  	
		if (phys:IsValid()) then  		
			local weld = constraint.Weld(Weapon, trace.Entity, 0, trace.PhysicsBone, 0)
			local nocollide = constraint.NoCollide(Weapon, trace.Entity, 0, trace.PhysicsBone)
			undo.Create("missileturret")
		undo.AddEntity( Weapon )
		undo.AddEntity( weld )
		undo.AddEntity( nocollide )
		undo.SetPlayer( ply )
	undo.Finish()
		end
		
	else
		local Weapon =  MakeMissile2(ply, Pos, Ang, Power)
		local phys = Weapon:GetPhysicsObject()  	
		if (phys:IsValid()) then  		
			local weld = constraint.Weld(Weapon, trace.Entity, 0, trace.PhysicsBone, 0)
			local nocollide = constraint.NoCollide(Weapon, trace.Entity, 0, trace.PhysicsBone)
			undo.Create("missileturret")
		undo.AddEntity( Weapon )
		undo.AddEntity( weld )
		undo.AddEntity( nocollide )
		undo.SetPlayer( ply )
	undo.Finish()
		end
		
	end

   return true
end

function TOOL:RightClick( trace )
	if (CLIENT) then return true end
	local ply = self:GetOwner()
	local Type = self:GetClientNumber( "Type" ) 
	if ( !trace.Hit ) then return end

	if ( SERVER and !util.IsValidPhysicsObject( trace.Entity, trace.PhysicsBone ) ) then return false end
			local Ang = (trace.HitNormal:Angle() + Angle(90,0,0))
			local Pos = trace.HitPos + trace.HitNormal* 8
	   local Power = self:GetClientNumber("power")

	if Type==0 then
	local Weapon =  MakeMissile(ply, Pos, Ang, Power)
	undo.Create("missileturret")
		undo.AddEntity( Weapon )
		undo.SetPlayer( ply )
	undo.Finish()
	else
	local Weapon =  MakeMissile2(ply, Pos, Ang, Power)
	undo.Create("missilelauncher")
		undo.AddEntity( Weapon )
		undo.SetPlayer( ply )
	undo.Finish()
	end
	
	
	
	
   return true
end



if (SERVER) then

	 function MakeMissile(pl, Pos, Ang, Pow)
		
			local missileturret = ents.Create("missileturret")
			missileturret:SetPos(Pos)
			missileturret:SetAngles( Ang)
			missileturret:Spawn()
			missileturret:Activate()
			missileturret:Setup(Pow)
			
			local ttable = {
					Pow = Pow;
						}
		table.Merge(missileturret:GetTable(), ttable )
		
			return missileturret
	end
	duplicator.RegisterEntityClass("missileturret", MakeMissile, "Pos", "Ang", "Pow", "frozen")
	
	 function MakeMissile2(pl, Pos, Ang, Pow)
		
			local missilelauncher = ents.Create("missilelauncher")
			missilelauncher:SetPos(Pos)
			missilelauncher:SetAngles( Ang)
			missilelauncher:Spawn()
			missilelauncher:Activate()
			missilelauncher:Setup(Pow)
			
			local ttable = {
					Pow = Pow;
						}
		table.Merge(missilelauncher:GetTable(), ttable )
		
			return missilelauncher
	end
	duplicator.RegisterEntityClass("missilelauncher", MakeMissile2, "Pos", "Ang", "Pow", "frozen")
end

if CLIENT then
function TOOL.BuildCPanel(panel)
   
   panel:AddControl("Header", {Text = "#Tool_missileturret_name", Description = "#Tool_missileturret_desc"})

   	local Mtype = {Label = "#Missile Type", MenuButton = 0, Options={}}
		Mtype["Options"]["#Seeking"]	= { missileturret_Type = "0" }
		Mtype["Options"]["#Guided"]	= { missileturret_Type = "1" }
		

	panel:AddControl("ComboBox", Mtype )

   
   panel:AddControl("Slider", {
      Label = "Power",
      Type = "Integer",
      Min = "10",
      Max = "200",
      Command = "missileturret_Power"})
end 
end 